import 'package:flutter/material.dart';
import 'package:huajianshe_app/base/BaseState.dart';
import 'package:huajianshe_app/pages/circle/CirclePage.dart';
import 'package:huajianshe_app/pages/contact/ContactPage.dart';
import 'package:huajianshe_app/pages/lesson/LessonPage.dart';
import 'package:huajianshe_app/pages/mine/MinePage.dart';
import 'package:huajianshe_app/widget/BottomNavWidget.dart';

import 'home_page.dart';

class MainPage extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return _MainPageState();
  }
}

class _MainPageState extends BaseState<MainPage> with SingleTickerProviderStateMixin {

  late PageController pageController;
  late ValueNotifier<int> currentIndex;
  late int index;

  @override
  void initState() {
    super.initState();
    // tabController = TabController(length: 2, vsync: this);
    pageController = PageController(initialPage: 0);
    index = 0;
    currentIndex = ValueNotifier(index);
  }

  @override
  void dispose() {
    // tabController.dispose();
    super.dispose();
  }

  void onPageChange(int index) {
    print('page changed ${currentIndex.value}');
    // setState(() => {});
  }

  @override
  Widget build(BuildContext context) {
    return Stack(children: [
      ValueListenableBuilder(
          valueListenable: currentIndex,
          builder: (BuildContext context, int value, Widget? child) {
            print('${currentIndex.value}');
            if (pageController.hasClients) { // 解决 PageController.page cannot be accessed before a PageView is built with it
              if (currentIndex.value != pageController.page?.floor()) {
                pageController.jumpToPage(currentIndex.value);
              }
            }
            return PageView.builder(
                key: const Key('pageView'),
                physics: NeverScrollableScrollPhysics(),
                itemCount: 5,
                onPageChanged: onPageChange,
                controller: pageController,
                // itemBuilder: (_, int index) => Text("1"),
                itemBuilder: (_, int index) {
                  switch(index) {
                    case 1: return LessonPage();
                    case 2: return CirclePage();
                    case 3: return ContactPage();
                    case 4: return MinePage();
                    case 0:
                    default:
                      return HomePage(mainIndexNotifier: currentIndex,);
                  }
                });
          }
      ),

      Positioned(child: ValueListenableBuilder(
        valueListenable: currentIndex,
        builder: (BuildContext context, int value, Widget? child) {
          return BottomNavWidget(
            selectedUrls: ['ic_home_main_selected', 'ic_home_lesson_selected', 'ic_home_circle_selected', 'ic_home_contact_selected', 'ic_home_mine_selected'],
            unselectedUrls: ['ic_home_main_unselected', 'ic_home_lesson_unselected', 'ic_home_circle_unselected', 'ic_home_contact_unselected', 'ic_home_mine_unselected'],
            titles: ['首页', '课程', '圈子', '人脉', '我的'],
            currentIndex: currentIndex,
            padding: EdgeInsets.symmetric(horizontal: 18),
          );
        }), left: 12, right: 12, bottom: 12,),
    ]);
  }


}